/*
 * Copyright (c) 2022 Kirky
 * Licensed under the Open Software Licence, Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://opensource.org/licenses/osl-3.0.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bei.lingluan.generalDialog;


import com.bei.lingluan.ResourceTable;
import com.bei.lingluan.utils.ResourceHelper;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.ComponentState;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.StateElement;
import ohos.app.Context;

/**
 * @author Kirky
 * @version 1.0
 * @description Tool class for dialog button background
 * @date 2021/12/10
 */
public class GeneralButtonElement extends StateElement {
    public static final int TYPE_NORMAL = 0;
    public static final int TYPE_ACCENT = 1;
    public static final int TYPE_ERROR = 2;
    private final Context mContext;
    private final int[] mStateFlagEmpty = new int[]{ComponentState.COMPONENT_STATE_EMPTY};
    private final int[] mStateFlagPressed = new int[]{ComponentState.COMPONENT_STATE_PRESSED};

    public GeneralButtonElement(Context context, int type) {
        super();
        mContext = context;
        int[][] mAllTypeColorIds = {
                {
                        ResourceTable.Color_color_normal, ResourceTable.Color_color_normal_pressed
                },
                {
                        ResourceTable.Color_colorAccent, ResourceTable.Color_color_accent_pressed
                },
                {
                        ResourceTable.Color_color_error, ResourceTable.Color_color_error_pressed
                }
        };
        initBackground(mAllTypeColorIds[type]);
    }

    private void initBackground(int[] colorIds) {
        this.addState(mStateFlagPressed, new ShapeElement() {
            {
                setRgbColor(RgbColor.fromArgbInt(ResourceHelper.getColorInt(mContext, colorIds[1], 0)));
                setCornerRadius(ResourceHelper.getFloat(mContext, ResourceTable.Float_dialog_corner_radius, 0));
            }
        });
        this.addState(mStateFlagEmpty, new ShapeElement() {
            {
                setRgbColor(RgbColor.fromArgbInt(ResourceHelper.getColorInt(mContext, colorIds[0], 0)));
                setCornerRadius(ResourceHelper.getFloat(mContext, ResourceTable.Float_dialog_corner_radius, 0));
            }
        });
    }
}
